package hotel.switchs.oversea.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.entity.SwitchHotelActiveTagEntity;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.ExcelUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.StringUtil;
import hotel.switchs.oversea.api.VO.request.ActiveTagVO;
import hotel.switchs.oversea.api.entity.SwitchHotelActiveTag;
import hotel.switchs.oversea.biz.mapper.SwitchHotelActiveTagMapper;
import hotel.switchs.oversea.biz.service.SwitchCreateRoomService;
import hotel.switchs.oversea.biz.service.SwitchHotelActiveTagService;
import hotel.switchs.oversea.biz.service.SwitchPushRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.Thread.sleep;

/**
 * @Author lmf
 * @Date 2020/8/17 17:02
 **/
@Service
@Slf4j
public class SwitchHotelActiveTagServiceImpl extends BaseServiceImpl<SwitchHotelActiveTagEntity> implements SwitchHotelActiveTagService {
    @Autowired
    SwitchHotelActiveTagMapper switchHotelActiveTagMapper;

    @Autowired
    SwitchPushRoomService switchPushRoomService;

    @Resource
    private SwitchCreateRoomService switchCreateRoomService;


    @Override
    public Result<Object> importActiveTag(MultipartFile file) {
        if (ObjectUtils.isEmpty(file)) {
            return new Result<>("上传的文件不能为空");
        }
        if (file.isEmpty()) {
            return new Result<>("文件为空");
        }
        List<SwitchHotelActiveTagEntity> switchHotelActiveTagEntities = new ArrayList<>();
        try {
            List<Object[]> objects = ExcelUtil.importExcel(file.getInputStream());
            ;
            if (ObjectUtils.isEmpty(objects) || objects.size() < 1) {
                return new Result<>("解析数据失败，解析出来的数据为空");
            }
            for (Object[] os : objects) {
                SwitchHotelActiveTagEntity switchHotelActiveTagEntity = new SwitchHotelActiveTagEntity();
                switchHotelActiveTagEntity.setId(StringUtil.getUUID());
                switchHotelActiveTagEntity.setHotelId(Integer.valueOf(os[1].toString()));
                switchHotelActiveTagEntity.setHotelName(os[2].toString());
                switchHotelActiveTagEntity.setRoomId(Integer.valueOf(os[3].toString()));
                switchHotelActiveTagEntity.setRoomName(os[4].toString());
                switchHotelActiveTagEntity.setSaleName(os[5].toString());
                String[] time = os[6].toString().split("至");
                switchHotelActiveTagEntity.setStartTime(time[0]);
                switchHotelActiveTagEntity.setEndTime(time[1]);
                switchHotelActiveTagEntity.setWeek(os[7].toString());
                String name = os[10].toString();
                //折扣
                if (name.contains("折")) {
                    switchHotelActiveTagEntity.setGenre(0);
                    switchHotelActiveTagEntity.setRate(BigDecimal.valueOf(Double.valueOf(name.replace("折", ""))));
                } else {//立减
                    switchHotelActiveTagEntity.setGenre(1);
                    switchHotelActiveTagEntity.setRate(BigDecimal.valueOf(Double.valueOf(name.replace("立减", ""))));
                }
                switchHotelActiveTagEntity.setRemark("");
                switchHotelActiveTagEntity.setConditions("");
                switchHotelActiveTagEntity.setCreateTime(new Date());
                switchHotelActiveTagEntity.setModifyTime(new Date());
                if (os.length > 11) {
                    switchHotelActiveTagEntity.setState(1);
                } else {
                    switchHotelActiveTagEntity.setState(0);
                }
                switchHotelActiveTagEntities.add(switchHotelActiveTagEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result<>("导入失败,跑出异常");
        }
        if (switchHotelActiveTagEntities.size() > 0) {
            switchHotelActiveTagMapper.insertList(switchHotelActiveTagEntities);
            return new Result<>("导入成功");
        }
        return new Result<>("导入失败");
    }

    /**
     * @author: lmf
     * @date: 2020/9/3 11:40
     * 重新推送含有价格标签的价格
     */
    @Override
    public Result<Object> pushActiveTagPrice() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String time = sdf.format(date);
        List<SwitchHotelActiveTagEntity> switchHotelActiveTagEntities = switchHotelActiveTagMapper.selectAllRoomId(time);
        int hotelId = 0;
        Map<Integer, List<Integer>> data = new HashMap<>();
        List<Integer> ids = new ArrayList<>();
        int i = 0;
        for (SwitchHotelActiveTagEntity switchHotelActiveTagEntity : switchHotelActiveTagEntities) {
            if(switchHotelActiveTagEntities.size()==1){
                hotelId = switchHotelActiveTagEntity.getHotelId();
                ids.add(switchHotelActiveTagEntity.getRoomId());
                data.put(hotelId, ids);
            }else{
                if (hotelId == 0) {
                    hotelId = switchHotelActiveTagEntity.getHotelId();
                    ids.add(switchHotelActiveTagEntity.getRoomId());
                    i++;
                } else {
                    i++;
                    if (hotelId == switchHotelActiveTagEntity.getHotelId()) {
                        ids.add(switchHotelActiveTagEntity.getRoomId());
                        if (i == switchHotelActiveTagEntities.size()) {
                            data.put(hotelId, ids);
                        }
                    } else {
                        data.put(hotelId, ids);
                        ids = new ArrayList<>();
                        hotelId = switchHotelActiveTagEntity.getHotelId();
                        ids.add(switchHotelActiveTagEntity.getRoomId());
                    }
                }
            }
        }
        log.info("=========data的长度为{}",data.size());
        for (Map.Entry<Integer, List<Integer>> entry : data.entrySet()) {
            try {
                sleep(20);
                log.info("==============key:为{},value为{}========", entry.getKey(), entry.getValue().size());
                switchPushRoomService.pushActiveTagPrice(entry.getKey(), entry.getValue());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        return new Result<>();
    }


    /**
     * <H1>新增活动信息</H1>
     *
     * @param activeTagVO 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/10 16:32
     */
    @Override
    public Result<Object> insertActive(ActiveTagVO activeTagVO) {
        String hotelIds = activeTagVO.getHotelIds();
        int[] hotelIdArr = Arrays.stream(hotelIds.split(",")).mapToInt(Integer::valueOf).toArray();
        List<SwitchHotelActiveTag> hotelRoomInfoList = switchCreateRoomService.getHotelRoomInfo(hotelIdArr);
        if (CollectionUtil.isNotEmpty(hotelRoomInfoList)) {
            for (SwitchHotelActiveTag switchHotelActiveTag : hotelRoomInfoList) {
                //日期类型
                if (activeTagVO.getDays() != 0) {
                    switchHotelActiveTag.setStartTime(DateUtil.getToday());
                    switchHotelActiveTag.setEndTime(DateUtil.getNextDays(activeTagVO.getDays()));
                } else {
                    switchHotelActiveTag.setStartTime(activeTagVO.getBeginTime());
                    switchHotelActiveTag.setEndTime(activeTagVO.getEndTime());
                }
                switchHotelActiveTag.setId(UUID.randomUUID().toString());
                switchHotelActiveTag.setRate(activeTagVO.getRate());
                switchHotelActiveTag.setState(0);
                switchHotelActiveTag.setSaleName(activeTagVO.getSaleName());
                switchHotelActiveTag.setUniKey(switchHotelActiveTag.getHotelId().toString() + switchHotelActiveTag.getRoomId());
            }
        }
        int integer = switchHotelActiveTagMapper.insertActive(hotelRoomInfoList);
        log.debug("调用方法 >>>>>>>>>>>>>>>>>>> insertActive <<<<<<<<<<<<<<<<<<<<<成功,插入数据: {} 条", integer);
        return new Result<>("插入成功", CommonConstants.SUCCESS);
    }

    /**
     * <H1>获取活动列表</H1>
     *
     * @param activeTagVO 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/11 14:36
     */
    @Override
    public Result<Object> activeInfoList(ActiveTagVO activeTagVO) {
        PageHelper.startPage(activeTagVO.getPageCode(), activeTagVO.getPageSize());
        List<SwitchHotelActiveTag> switchHotelActiveList = switchHotelActiveTagMapper.activeInfoList(activeTagVO);
        PageInfo<SwitchHotelActiveTag> pageInfo = new PageInfo<>(switchHotelActiveList);
        return new Result<>(pageInfo);
    }

}
